import QtQuick 1.0
import "Components"
import "WebAccess/TwitterAPI.js" as TwitterAPI

Rectangle {
    x: 0
    y: 0
    width: parent.width
    height: parent.height

    color: "#111111"

    property alias text: _textTweet.text        //入力されているテキストへのアクセス
    property string inReplyToId: ""             //replyの時の情報
    property string inReplyToScreenName: ""     //replyの対象のスクリーンネームこれがツイーとに含まれなければ削除


    TextBox {
        id: _textTweet
        anchors.left: parent.left
        anchors.top: parent.top
        anchors.right: _buttonTweet.left
        anchors.bottom: parent.bottom

        anchors.leftMargin: 5
        anchors.topMargin: 5
        anchors.rightMargin: 5
        anchors.bottomMargin: 5

        onTextChanged: {
            if(text.length > 0){
                if( _rootContainer.state === "NoneTweet"){
                    _rootContainer.state = "";
                }
            }
            if(textActiveFocus === false){
                var pos = _textTweet.text.indexOf("\t");
//                console.debug(_textTweet.text + ",pos=" + pos + ",len=" + _textTweet.text.length);
                if(pos === _textTweet.text.length - 1){
                    _textTweet.text = _textTweet.text.substr(0, _textTweet.text.length - 1);
                }
            }

            _buttonTweet.text = "Tweet\n(" + (140 - text.length) + ")";

        }

        onPressKey:{
            if(key === Qt.Key_Tab){
                _buttonTweet.forceActiveFocus();
            }
//            if(key === Qt.Key_Enter){
//                sendTweet();
//            }
        }
    }

    Button {
        id: _buttonTweet
        anchors.top: parent.top
        anchors.right: parent.right
        anchors.bottom: parent.bottom
//        anchors.leftMargin: 5
        anchors.topMargin: 5
        anchors.rightMargin: 5
        anchors.bottomMargin: 5
        width: 50

        text: "Tweet\n(140)"

        onClicked: {
            sendTweet();
        }

        onPressKey:{
            console.debug("button:key="+key);
            switch(key){
            case Qt.Key_Return:
            case Qt.Key_Enter:
                sendTweet();
                focus = false;
                break;
            case Qt.Key_Tab:
                focus = false;
                _textTweet.setForceActiveFocus();
                break;
            default:
                break;
            }
        }
    }

    //つぶやく
    function sendTweet(){
        if(_textTweet.text.lenght === 0){
        }else{
            //in reply to が指定されてるのにツイート内容にIDがなければIDは削除
            if(inReplyToId.length > 0){
                if(_textTweet.text.indexOf(inReplyToScreenName) >= 0){
                    //含まれる
                }else{
                    inReplyToId = "";
                }
            }
            //つぶやく
            var obj = new TwitterAPI.sendTweet(_textTweet.text, inReplyToId, onTweetResponse);
        }
    }

    //つぶやきのレスポンス
    function onTweetResponse(object){
        console.debug("onTweetResponse")
        _textTweet.text = "";
        //        _textTweet.forceActiveFocus();

        //トースト表示
        _toast.show("Sent")
    }
}
